<?php
require_once('../../carbox.php');
if(!carbox_check_session( ))
	header('Location: http://'.$_SERVER['HTTP_HOST'].'/carbox/login.php');
$sql = "
	SELECT COUNT(p.id) AS pages
	FROM Page p
	INNER JOIN Document AS d ON p.document_id = d.id
	INNER JOIN DocumentType AS dt ON d.type_id = dt.id
	WHERE d.process_id IN ('".implode("','", $_POST['pids'])."')
";
$rs = carbox_query($sql);
if($rs['result'][0]['pages'] == 0) {
	die('
		<div class="alert alert-block alert-error fade in">
			<button type="button" class="close" data-dismiss="alert">×</button>
			<p class="alert-heading">Este processo não possui imagens digitalizadas!</p>
          	</div>
	');
}
?>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span3">
			<div class="accordion" id="accordion-docs">
			<?php
			for($k = 0; $k < count($_POST['pids']); ++$k) {
				$pn = carbox_get_pn_by_pid($_POST['pids'][$k]);
				$sql = "
					SELECT COUNT(p.id) AS pages
					FROM Page p
					INNER JOIN Document AS d ON p.document_id = d.id
					INNER JOIN DocumentType AS dt ON d.type_id = dt.id
					WHERE d.process_id = :pid
					GROUP BY d.process_id
				";
				$params = array(':pid' => $_POST['pids'][$k]);
				$proc_pages = carbox_query($sql, $params);
				$proc_pages = $proc_pages['result'][0]['pages'];
				$sql = "
					SELECT d.id, d.process_id, dt.type, COUNT(p.id) AS pages
					FROM Page p
					INNER JOIN Document AS d ON p.document_id = d.id
					INNER JOIN DocumentType AS dt ON d.type_id = dt.id
					WHERE d.process_id = :pid
					GROUP BY d.id
					ORDER BY IFNULL(p.number_absolute, d.type_id)
				";
				$params = array(':pid' => $_POST['pids'][$k]);
				$query = end(carbox_query($sql, $params));
			?>
				<div class="accordion-group">
					<div class="accordion-heading">
						<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion-docs" href="#collapse<?=$k?>"><?=$pn?></a>
					</div>
					<div id="collapse<?=$k?>" class="accordion-body collapse" style="height: 0px; ">
						<div class="accordion-inner">
							<ul class="nav nav-pills nav-tabs nav-stacked">
								<li class="dropdown">
									<a href="#" class="dropdown-toggle" data-toggle="dropdown">Todos <b class="caret pull-right"></b></a>
									<ul class="dropdown-menu">
										<li><a href="#id=<?=$_POST['pids'][$k]?>&mode=PROC_PAGE" qt-pages="<?=$proc_pages?>">Por Página</a></li>
									</ul>
								</li>
								<?php
								for($i = 0; $i < count($query); ++$i)
									echo '
										<li>
											<a href="#id='.$query[$i]['id'].'&mode=ONLY_DOC" qt-pages="'.$query[$i]['pages'].'">'.$query[$i]['type'].' ('.$query[$i]['pages'].'p.)</a>
										</li>
									';
								?>
							</ul>
						</div>
					</div>
				</div>
			<?php			
			}
			?>
			</div>
		</div>
		<div class="span9" >
			<div class="shadowinner" id="viewport"></div>
			<div class="pagination pagination-centered">
				<?php
				$sc = cerberus_query("SELECT sigla_categoria FROM Usuario WHERE usuario_id = '".carbox_get_cerberus_uid($_SESSION["uid"])."'");
				$sc = $sc['result'][0]["sigla_categoria"];
				if($sc != 'AT') {
					echo '
						<ul>
							<li><a href="#" id="btn-print" title="Imprimir"><i class="icon-print"></i></a></li>
							<li><a href="#" id="btn-save" title="Salvar"><i class="icon-download-alt"></i></a></li>
						</ul>
					';
				}
				?>
				<ul>
					<li><a href="#" id="btn-width-adjust" title="Ajustar à largura"><i class="icon-resize-horizontal"></i></a></li>
					<li><a href="#" id="btn-height-adjust" title="Ajustar à altura"><i class="icon-resize-vertical"></i></a></li>
				</ul>
				<ul>
					<li><a href="#" id="btn-first-page" title="Primeira página"><i class="icon-step-backward"></i></a></li>
					<li><a href="#" id="btn-previous-page" title="Página anterior"><i class="icon-chevron-left"></i></a></li>
					<li><a href="#"><input class="span12" id="input-page" style="text-align: center;" title="Página atual" /></a></li>
					<li><a href="#" id="btn-next-page" title="Próxima página"><i class="icon-chevron-right"></i></a></li>
					<li><a href="#" id="btn-last-page" title="Última página"><i class="icon-step-forward"></i></a></li>
				</ul>
				<ul>
					<li><a href="#" id="btn-zoom-in" title="Aumentar zoom"><i class="icon-zoom-in"></i></a></li>
					<li><a href="#" id="btn-zoom-out" title="Diminuir zoom"><i class="icon-zoom-out"></i></a></li>
				</ul>
				<ul>
					<li><a href="#" id="btn-rotate" title="Girar"><i class="icon-repeat"></i></a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript"> $(carbox_ui_view( )); </script>
